package dao

import (
	"context"
	"sale-admin/internal/dal"
	"sale-admin/internal/models"
	"time"

	"gorm.io/gorm"
)

type WechatUser struct{}

func (WechatUser) Create(ctx context.Context, db *gorm.DB, NickName, headImg, openID string) error {
	m := dal.Use(db).MaWechatUser
	tx := m.WithContext(ctx)
	tx.Create(&models.MaWechatUser{
		NickName:  NickName,
		Image:     headImg,
		OpenID:    openID,
		AddTime:   time.Now(),
		CreatedAt: time.Now().Unix(),
	})
	return nil
}

func (WechatUser) Detail(ctx context.Context, db *gorm.DB, phone, openid string) (info *models.MaWechatUser, err error) {
	m := dal.Use(db).MaWechatUser
	tx := m.WithContext(ctx)
	if phone != "" {
		tx = tx.Where(m.Phone.Eq(phone))
	}
	if openid != "" {
		tx = tx.Where(m.OpenID.Eq(openid))
	}
	info, err = tx.Take()
	return
}
